//
//  SelectLocationViewController.m
//  futureLive
//
//  Created by zhoubiwen on 15/11/18.
//  Copyright © 2015年 ZHOU_LI_SUN. All rights reserved.
//

#import "SelectLocationViewController.h"
#import "NetWorkManager.h"
#import "SLCityModel.h"
#import "SLMoreCitysModel.h"

@interface SelectLocationViewController ()
@property (nonatomic, strong) NSMutableArray *hotlistArray;
@property (nonatomic, strong) NSMutableArray *listArray;
@end

@implementation SelectLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initSubViews];
}

/**
 *  初始化数据
 */
- (void)initData {
  
    [NetWorkManager getJsonDataWithURL:selectLocationURL success:^(id data) {
        self.hotlistArray =  [NSMutableArray new];
        self.listArray = [NSMutableArray new];
        NSArray *hotlist = data[@"data"][@"hotlist"];
        for (NSDictionary *hotlistDict in hotlist) {
            SLCityModel *model = [[SLCityModel alloc]init];
            [model setValuesForKeysWithDictionary:hotlistDict];
            [self.hotlistArray addObject:model];
        }
        NSArray *list = data[@"data"][@"list"];
        for (NSDictionary *listDic in list) {
            SLMoreCitysModel *model = [[SLMoreCitysModel alloc]init];
            [model setValuesForKeysWithDictionary:listDic];
            [self.listArray addObject:model];
        }
        
        [self.tableView reloadData];
        
    } fail:^{
        NSLog(@"城市信息获取失败.");
        }];
}
/**
 *  初始化子视图
 */
- (void)initSubViews {
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:self.leftBarButtonItemTitle style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction:)];
    
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 50)];
//    view.backgroundColor = [UIColor redColor];
//    self.tableView.tableHeaderView = view;
    
}

- (void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return self.listArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    SLMoreCitysModel *model = self.listArray[section];
    
    return model.city.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    SLMoreCitysModel *model = self.listArray[indexPath.section];

    cell.textLabel.text = [model.city[indexPath.row] cityName];
    
    // Configure the cell...
    
    return cell;
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    NSMutableArray *array = [NSMutableArray new];
    for (int i = 65; i < 91; i++) {
        NSString *string = [NSString stringWithFormat:@"%c",i];
        [array addObject:string];
    }
    return array;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
   return [self.listArray[section] areaName];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SLMoreCitysModel *moreModel = self.listArray[indexPath.section];
    SLCityModel *cityModel = moreModel.city[indexPath.row];
    _myBlock(cityModel);
    [self.navigationController popViewControllerAnimated:YES];

}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
